package com.weigang.webservice.controller;

import com.weigang.webservice.dto.User;
import com.weigang.webservice.exception.R;
import com.weigang.webservice.interceptor.ClientLoginInterceptor;
import com.weigang.webservice.service.UserAuthInterceptorServer;
import com.weigang.webservice.service.UserService;
import com.weigang.webservice.utils.IDUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.namespace.QName;
import java.util.List;

@RestController
@RequestMapping({"webservice"})
public class WebServiceController {


    @GetMapping({"queryUser/{str}"})
    public R queryUser(@PathVariable("str") String str) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress("http://localhost:2222/ws/userService?wsdl");
        factory.setServiceClass(UserService.class);
        UserService userService = factory.create(UserService.class);
        Client client = ClientProxy.getClient(userService);
        Endpoint cxfEndpoint = client.getEndpoint();
        cxfEndpoint.getOutInterceptors().add(new LoggingOutInterceptor());
        System.out.println(userService.getClass());
        User user = userService.queryUser("sadas", str);
        System.out.println(user.toString());
        return R.ok().put("data", user);
    }

    @PostMapping({"queryUser"})
    public CheckResponse queryUser(@RequestBody CheckRequest checkRequest) {
        System.out.println(111111);
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress("http://localhost:8080/soap/lowValue?wsdl");
        factory.setServiceClass(LowValueService.class);
        LowValueService userService = factory.create(LowValueService.class);
        Client client = ClientProxy.getClient(userService);
        Endpoint cxfEndpoint = client.getEndpoint();
        cxfEndpoint.getOutInterceptors().add(new LoggingOutInterceptor());
        return userService.checkMatName(checkRequest);
    }


    @GetMapping({"queryUserAuthInterceptor/{str}"})
    public R queryUserAuthInterceptor(@PathVariable("str") String str) {
        try{
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            factory.setAddress("http://localhost:2222/ws/userAuthInterceptorServer?wsdl");
            factory.setServiceClass(UserAuthInterceptorServer.class);
            UserAuthInterceptorServer userAuthInterceptorServer = factory.create(UserAuthInterceptorServer.class);
            Client client = ClientProxy.getClient(userAuthInterceptorServer);
            Endpoint cxfEndpoint = client.getEndpoint();
            cxfEndpoint.getOutInterceptors().add(new LoggingOutInterceptor());
            //用户认证
            cxfEndpoint.getOutInterceptors().add(new ClientLoginInterceptor("admin", "123456"));
            System.out.println(userAuthInterceptorServer.getClass());
            User user = userAuthInterceptorServer.queryUserAuthInterceptor(str, str);
            System.out.println(user.toString());
            return R.ok().put("data", user);
        }catch (Exception e){
            return R.error(e.getMessage());
        }

    }



//    @GetMapping({"demo/{str}"})
//    public R demo(@PathVariable("str") String str) {
//        try {
//            String address = "http://192.168.50.24:8080/ws/userAuthInterceptorServer?wsdl";
//            UserAuthInterceptorServer userAuthInterceptorServer = (UserAuthInterceptorServer)JaxWsProxyFactory.clientFactory(address, UserAuthInterceptorServer.class, new Interceptor[]{new ClientLoginInterceptor("admin", "123456")});
//            User user = userAuthInterceptorServer.queryUserAuthInterceptor("sadas", str);
//            System.out.println(user.toString());
//            return R.ok().put("data", user);
//        } catch (Exception var5) {
//            return R.error(var5.getMessage());
//        }
//    }
}
